package com.laoxiang.modules.system.controller;


import com.laoxiang.modules.system.entity.SysUser;
import com.laoxiang.modules.system.entity.dto.PageDTO;
import com.laoxiang.modules.system.entity.vo.RestPassWordVO;
import com.laoxiang.modules.system.entity.vo.SysUserVO;
import com.laoxiang.modules.system.entity.vo.UpdateUserStatusVO;
import com.laoxiang.modules.system.entity.vo.UserInfoRolePostVO;
import com.laoxiang.modules.system.service.SysUserService;
import com.laoxiang.utils.AppHttpCodeEnum;
import com.laoxiang.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;


/**
 * 系统用户表(SysUser)表控制层
 *
 * @author makejava
 * @since 2023-07-13 11:37:44
 */
@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户管理")
public class SysUserController  {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @ApiOperation(httpMethod = "GET", value = "用户列表")
    @GetMapping("/getUserList")
    public ResponseResult getUserList(Integer pageNum, Integer pageSize, SysUserVO sysUserVO){
        PageDTO userList = sysUserService.getUserList(pageNum, pageSize, sysUserVO);
        return ResponseResult.okResult(userList);
    }

    @ApiOperation(httpMethod = "POST", value = "新增用户")
    @PostMapping("/addUser")
    public ResponseResult addUser(@RequestBody SysUser sysUser){
        int count = sysUserService.addUser(sysUser);
        if(count>0){
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.USER_INSET_ERROR);
        }
    }

    @ApiOperation(httpMethod = "GET",value = "通过id获得用户信息")
    @GetMapping("/getUserById/{userId}")
    public ResponseResult getUserById(@PathVariable("userId") Long userId){
        UserInfoRolePostVO userInfoRolePostVO = sysUserService.getUserById(userId);
        return ResponseResult.okResult(userInfoRolePostVO);
    }

    @ApiOperation(httpMethod = "PUT",value = "修改用户")
    @PutMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody SysUser sysUser){
        sysUserService.updateUser(sysUser);
        return ResponseResult.okResult();
    }

    @ApiOperation(httpMethod = "DELETE",value = "通过id删除用户")
    @DeleteMapping("/deleteUserById/{userId}")
    public ResponseResult deleteUser(@PathVariable("userId") Long userId){
        sysUserService.deleteUser(userId);
        return ResponseResult.okResult();
    }

    @ApiOperation(httpMethod = "DELETE",value = "批量删除用户信息")
    @DeleteMapping("/bachDeleteUser/{ids}")
    public ResponseResult bachDeleteUser(@PathVariable("ids") List<Long> ids){
        sysUserService.bachDeleteUser(ids);
        return ResponseResult.okResult();
    }

    @ApiOperation(httpMethod = "PUT",value = "修改用户状态")
    @PutMapping("/updateUserStatus")
    public ResponseResult updateUserStatus(@RequestBody UpdateUserStatusVO updateUserStatusVO){
        sysUserService.updateUserStatus(updateUserStatusVO);
        return ResponseResult.okResult();
    }

    @ApiOperation(httpMethod = "PUT",value = "重置密码")
    @PutMapping("/restPassWord")
    public ResponseResult restPassWord(@RequestBody RestPassWordVO restPassWordVO){
        sysUserService.restPassWord(restPassWordVO);
        return ResponseResult.okResult();
    }

}

